#tool
extends RigidBody
enum{jump,climb,attack}
export var jump_height:int
var shot=load("res://shot.tscn")
var move_speed=5

var life=3
var walk_vec=1
var max_speed=8
var shot_item
#是否可以计算距离
var can_caculate_distance=false
var action=""
signal died(life)
signal open_chest
#计数
var clicks=0
var debug_input=true
func _ready():
	connect("tree_exited",get_tree().current_scene.get_node("."),"died")
	jump_height=10
	var os=OS.get_name()
	if os=="Android":
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	if os=="Windows":
		if debug_input:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		else:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	pass
#计算到子弹的距离
func caculate_distance_to_shot(item_shot):
	var distance=self.translation.distance_to(item_shot.translation)
	print("子弹到人物的距离",distance)
	if distance>20:
		can_caculate_distance=false
		shot_item.queue_free()
func _process(delta):
	if action=="climb":
		$Sprite3D.play("climb")
	if Input.is_action_just_pressed("f3"):
		get_tree().set_debug_collisions_hint(true)
		print_debug("碰撞箱显示on")
	if Input.is_action_just_pressed("f4"):
		get_tree().set_debug_collisions_hint(false)
		print_debug("碰撞箱显示off")
	if action=="attack":
		shoot()
	if Input.is_action_just_pressed("k") or action=="jump":
		jump_def()
	if Input.is_action_pressed("k"):
		if action=="climb":
			translate(Vector3(0,5*delta,0))
			pass
	if Input.is_action_pressed("a") or action=="left":
		#add_torque(Vector3(0,5,0))
		#print("添加扭矩")
		self.rotation_degrees.y+=100*delta
	if Input.is_action_pressed("d") or action=="right":
		self.rotation_degrees.y-=100*delta
	if Input.is_action_pressed("w") or action=="walk":
		$Sprite3D.play("walk")
		#print_debug("speed",walk_vec)
		#print_debug("vec",self.linear_velocity)
		if walk_vec<max_speed:
			walk_vec+=1
		apply_central_impulse(Vector3(0,0,-walk_vec*delta).rotated(Vector3(0,1,0),deg2rad(self.rotation_degrees.y)))
	if Input.is_action_pressed("s") or action=="walkback":
		$Sprite3D.play("walk")
		#print_debug("speed",walk_vec)
		apply_central_impulse(Vector3(0,0,walk_vec*delta).rotated(Vector3(0,1,0),deg2rad(self.rotation_degrees.y)))
	if Input.is_action_just_pressed("r"):
# warning-ignore:return_value_discarded
		Global.score=0
		life=3
		get_tree().reload_current_scene()
	if Input.is_action_just_released("w"):
		$Sprite3D.stop()
	if Input.is_action_just_released("s"):
		$Sprite3D.stop()
	#有bug
	if self.translation.y<-50:
		#消除时发出信号 复活
		get_tree().reload_current_scene()
		#queue_free()
func died_sound():
	$hit.play(0.51)
#		life-=1
#		emit_signal("died",life)
#		set_process(false)
	#print(self.translation.y)
	pass
func _on_switch_cam_toggled(button_pressed):
	if button_pressed:
		$fps_cam.make_current()
	else:
		$third_cam.make_current()
	pass # Replace with function body.
func _on_player_body_entered(body):
	#print(body.name)
	if "star" in body.name:
		body.queue_free()
		$coin.play()
	if "enemy" in body.name:
		life-=1
		body.queue_free()
		$hit.play()
		emit_signal("died",life)
		if life==0:
			get_tree().current_scene.get_node("failed").show()
	if "chest" in body.name:
		print_debug("碰到箱子")
		emit_signal("open_chest")
		body.queue_free()
	if "tizi" in body.name:
		action="climb"
		print_debug("碰到梯子变更状态机"+action)
	pass 
func _on_hit_finished():
	pass 
func _on_player_body_exited(body):
	#print("碰撞箱离开了",body.name)
	if "tizi" in body.name:
		action="walk"
		print_debug("状态机为"+action)
	pass 
var mouse_sense=0.1
func _input(event):
	if event.is_action_pressed("j"):
		shoot()
	if event.is_action_pressed("exit"):
		get_tree().quit()
	if event is InputEventMouseMotion:
		#rotate_x(deg2rad(-event.relative.x*mouse_sense))
		rotate_y(deg2rad(-event.relative.x*mouse_sense))
	if event.is_action_pressed("v"):
		clicks+=1
		if clicks%2==1:
			$fps_cam.make_current()
		if clicks%2==0:
			$third_cam.make_current()
	pass
# warning-ignore:function_conflicts_constant
func attack():
	action="attack"
# warning-ignore:function_conflicts_constant
func jump():
	action="jump"
	#print_debug(action)
func walk():
	action="walk"
	#print_debug(action)
func walkback():
	action="walkback"
func stopwalk():
	action=""
func rotate_right():
	action="right"
	pass
func rotate_left():
	action="left"
	pass
func stop_rotate():
	action=""
	pass
func shoot():
	shot_item=shot.instance()
	shot_item.angle=self.rotation_degrees.y
	$shot_pos.add_child(shot_item)
	action=""
	pass
func jump_def():
	apply_impulse(Vector3(0,0,0),Vector3(0,jump_height,0))
	action=""
	pass
